package edu.whu;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;

public class ReflectionDemo {
    public static String getClassNama(String propertiesName){
        Properties props = new Properties();
        String className = "";
        try (InputStream input = ReflectionDemo.class.getResourceAsStream(propertiesName)) {
            if (input == null) {
                return "";
            }
            props.load(input);
            className = props.getProperty("bootstrapClass");
            //System.out.println(props.getProperty("bootstrapClass"));
            return className;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public static Class getClass(String className) throws ClassNotFoundException {
        Class c = Class.forName(className);
        return c;
    }

    public static Object objectConstruct(String className ) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class c = getClass(className);
        Constructor[] constructs = c.getDeclaredConstructors();//确保有无参构造器
        for (Constructor construct : constructs) {
            if(construct.getParameterCount() == 0){
                Object obj = construct.newInstance();
                return obj;
            }else{
                break;//调用有参构造器要有type
            }
        }
        return null;
    }

    public static void getMethod(Class c,String name) throws NullPointerException{
        try {
            Constructor construct = c.getDeclaredConstructor();//如果没有无参构造器
            Method[] methods = c.getDeclaredMethods();
            for (Method method : methods) {
                if(method.isAnnotationPresent(InitMethod.class)){
                    //若方法的形参列表是多个且不只一个方法？
                    method.setAccessible(true);
                    String methodName = method.getName();
                    Class[] parameterTypes = method.getParameterTypes();
                    int parameterCount = method.getParameterCount();
                    for (int i = 0; i < parameterTypes.length; i++) {
                        System.out.print(parameterTypes[i]);
                    }
                    if(parameterTypes.length == 0){
                        method.invoke(construct.newInstance());
                    }else {
                        if(name.isEmpty() != true){
                        method.invoke(construct.newInstance(),name);
                       }
                    }
                }
            }
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }

    }

}

